//
//  MusicViewController.m
//  WYH_MusicPlayer
//
//  Created by wyh on 16/3/30.
//  Copyright © 2016年 吴岳恒. All rights reserved.
//

#import "MusicViewController.h"
#import "UIImageView+WebCache.h"
#import "musicModel.h"
#import "MusicDetailViewController.h"
#import "MusicTableViewCell.h"
#import "AudioPlayer.h"
@interface MusicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *musicArr;

@property (nonatomic, assign) NSInteger i;
@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.i = -1;
    
    self.navigationController.navigationBar.alpha = 0.5;

    [self readMusicURL];
}

-(void)readMusicURL{
    
    self.musicArr = [NSMutableArray new];
    
    //子线程上请求数据,回到主线程刷新数据
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"];
        
        NSMutableArray *array = [NSMutableArray arrayWithContentsOfURL:url];
        
        NSLog(@"%@",[NSThread currentThread]);
        
        for (NSDictionary *dic in array) {
            musicModel *model = [musicModel musicModelWithDic:dic];
            [self.musicArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableview reloadData];
        });
    });

}
//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musicArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = [self.musicArr objectAtIndex:indexPath.row];
    
//    if ([AudioPlayer shareInstance].player.isPlaying == YES) {
//        NSInteger index = [AudioPlayer shareInstance].index;
//        if (indexPath.row == index) {
//            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(300, 50, 30, 30)];
//            image.image = [UIImage imageNamed:@"iconfont-yinlemusic216"];
//            [cell.contentView addSubview:image];
//        }
//    }
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor clearColor];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(330, 20, 30, 30)];
    image.image = [UIImage imageNamed:@"iconfont-yinlemusic216"];
    [view addSubview:image];
    cell.selectedBackgroundView = view;
    
    return cell;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    MusicDetailViewController *detailVC = segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow];
    //传入数组
    detailVC.musicArray = self.musicArr;
    
    detailVC.block = ^(NSInteger index){
        dispatch_async(dispatch_get_main_queue(), ^{
            self.i = index;
        });
        
        
    };
    
    NSLog(@"%ld",self.i);
    
    //获取当前模型
    musicModel *model = [self.musicArr objectAtIndex:indexPath.row];

    
    if (indexPath.row != self.i) {
//        dispatch_queue_t queue = dispatch_queue_create("com.wyh", DISPATCH_QUEUE_CONCURRENT);
//        dispatch_async(queue, ^{
//            {
                //播放器播放
            [detailVC musicPlayWith:model.mp3Url];
//            }
//            
//        });

    
    }
    
    //传入计数器
    detailVC.index = indexPath.row;
    
    
    

}


@end
